package org.hot100.h064;

/**
 * @Author: wjy
 * @Date: 2021/12/27 9:33
 */
public class Solution {

    /**
     * 给定一个包含非负整数的 m x n 网格 grid ，请找出一条从左上角到右下角的路径，使得路径上的数字总和为最小。
     *
     * 说明：每次只能向下或者向右移动一步。
     *
     * 示例 1：
     *
     * 输入：grid = [[1,3,1],[1,5,1],[4,2,1]]
     * 输出：7
     * 解释：因为路径 1→3→1→1→1 的总和最小。
     *
     * 示例 2：
     *
     * 输入：grid = [[1,2,3],[4,5,6]]
     * 输出：12
     *
     * @param args
     */
    public static void main(String[] args) {

        int[][] ints = {{1, 3, 1}, {1, 5, 1}, {4, 2, 1}};

        Solution solution = new Solution();
        System.out.println(solution.minPathSum(ints));

    }

    public int minPathSum(int[][] grid) {
        if (grid.length == 0) {
            return 0;
        }

        int temp = 0;

        for (int i = 0; i < grid.length; i++) {
            if (i > 0) {
                temp = grid[i - 1][0];
            }
            for (int  j = 0; j < grid[0].length; j++) {
                if (j > 0 && i > 0) {
                    grid[i][j] += Math.min(grid[i - 1][j], grid[i][j - 1]);
                } else {
                    grid[i][j] += temp;
                }
                temp = grid[i][j];
            }
        }

        return grid[grid.length - 1][grid[0].length - 1];
    }

/*    public int minPathSum(int[][] grid) {
        for(int i = 0; i < grid.length; i++) {
            for(int j = 0; j < grid[0].length; j++) {
                if(i == 0 && j == 0) continue;
                else if(i == 0)  grid[i][j] = grid[i][j - 1] + grid[i][j];
                else if(j == 0)  grid[i][j] = grid[i - 1][j] + grid[i][j];
                else grid[i][j] = Math.min(grid[i - 1][j], grid[i][j - 1]) + grid[i][j];
            }
        }
        return grid[grid.length - 1][grid[0].length - 1];
    }*/

}
